package org.jeecg.modules.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.erp.goods.entity.ErpGoods;
import org.jeecg.modules.erp.goods.entity.ErpGoodsCategory;
import org.jeecg.modules.erp.goods.mapper.ErpGoodsCategoryMapper;
import org.jeecg.modules.erp.goods.mapper.ErpGoodsMapper;
import org.jeecg.modules.storage.entity.BusinessType;
import org.jeecg.modules.storage.mapper.BusinessTypeMapper;
import org.jeecg.modules.storage.service.IBusinessTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 业务类型
 * @Author: jeecg-boot
 * @Date:   2024-07-01
 * @Version: V1.0
 */
@Service
public class BusinessTypeServiceImpl extends ServiceImpl<BusinessTypeMapper, BusinessType> implements IBusinessTypeService {

    @Autowired
    private ErpGoodsMapper erpGoodsMapper;


    @Autowired
    private ErpGoodsCategoryMapper erpGoodsCategoryMapper;

    /**
     * 获取业务类型
     * @param businessNode
     * @param businessType
     * @param matericalId
     * @return
     */
    @Override
    public BusinessType queryByMaterical(String businessNode, String businessType, String matericalId) {
        ErpGoods erpGoods = erpGoodsMapper.selectById(matericalId);
        ErpGoodsCategory cateory = this.getCateory(erpGoods.getCategoryCode());
        LambdaQueryWrapper<BusinessType> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(BusinessType::getBusinessNode,businessNode)
                .eq(BusinessType::getBusinessType,businessType)
                .eq(BusinessType::getMaterialTypeCode, cateory.getCode());
        return this.baseMapper.selectOne(queryWrapper);
    }


    /**
     * 获取分类
     * @param id
     * @return
     */
    private ErpGoodsCategory getCateory(String code){
        ErpGoodsCategory erpGoodsCategory = erpGoodsCategoryMapper.selectOne(new LambdaQueryWrapper<ErpGoodsCategory>()
                .eq(ErpGoodsCategory::getCode, code));
        if(!erpGoodsCategory.getParentId().equals("0")){
            ErpGoodsCategory category = erpGoodsCategoryMapper.selectById(erpGoodsCategory.getParentId());
            return this.getCateory(category.getCode());
        }
        return erpGoodsCategory;
    }

}
